#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

using namespace std;

int main()
{
    pid_t pid;
    int status;

    pid = fork();
    if (pid < 0)
    {
        cerr << "fork error" << endl;
        return 1;
    }
    else if (pid == 0)
    {
        // 子进程
        cout << "I am child process " << getpid() << endl;
        sleep(1);
        exit(0);
    }
    else
    {
        // 父进程
        cout << "I am parent process " << getpid() << endl;
        waitpid(pid, &status, 0);
        cout << "Child process terminated with status " << status << endl;
    }

    return 0;
}   
